load("AdHarmony/build.js");
/*var A = "var a='a';";
var B = "//=require A;var b='b';";
var C = "var c='c'";
var D = "//=require A;//=require B;//=require C;var d='d';";
var E = "//=require D; var e=a+b+c+d;"

var code = "\
//=require E\
//=require A\
\n\nalert(e == 'abcd')"
*/
importPackage( java.io );
importPackage( java.net );
importPackage( java.util );
importPackage( java.lang );






Koncat = (function(_global){
	var OS = java.lang.System.getProperty("os.name").toLowerCase();
	_global=_global||this;
		
	var finished=false;
	var reg =/\/\/\=\s*require\s([A-Za-z\.\/\\]*)/im;
	var usages={};
	
	var Koncat = {
	    Build: function(source, savePath){
			var code="";
			if(source instanceof File) {
				code = Koncat.Read(file);
			}
			else {
				code = source;
			}
			
	        do {
				code = code.replace(reg,function(a,b,c,d){
					b = Koncat.ResolveReference(b);
					
				    if(Koncat.File(b) && !usages[b]){
				       usages[b] = true;
				       code = Koncat.Read(Koncat.File(b))+"\n";
				    }
				    else if(!usages[b]){
				        print("Missing file reference to a class : " + b + ". Not found in any loadpaths.")
				        code = "";
				    }
				    else {code = "";}
				    return code;
				});
				if(!reg.test(code)){finished=true;}
			}
			while(finished!=true);
			
			if(typeof savePath == "string"){
				print("Building...");
				setTimeout(function(){ Koncat.Write(code, savePath) }, 1000);
			}
	        return code
	    },
		ResolveReference : function(b){
		
			var path="";
			if(/\.js$/.test(b) && /\//g.test(b)) {
				path=buildconfig.ApplicationFolderName + "/" + b;
		        //print(path)
		    }
		    else if(/\.js$/.test(b) && !/\//g.test(b)) {
		        b = b.replace(/\.js$/, "");
		        path = buildconfig.Libraries + b + ".js";
				
		    }
		    else{
				//print(b)
		        path = b.replace(/\./ig,"/") + ".js";
		    }
			
			return path;
		},
		Read : function(file) {
	      var stream, line, lineno,lines=[];
	      if (!BufferedReader || !FileReader) {
	        throw new Error("This operation requires Mozilla Rhino.");
	      }
	      stream = new BufferedReader(new FileReader(file));
	      lineno = 1;
	      while ((line = stream.readLine()) !== null) {
	        lines.push(line);
	      }
	      return lines.join("\n");
	    },
        File : function find(filename){
			//print("path:" + Koncat.ResolveReference(filename));
			//filename = Koncat.ResolveReference(filename);
			filename = filename.replace(/\.js$/, "") + ".js";
            var  file, location, index = 0;
            while ((location = buildconfig.LoadPaths[index++])) {
				file = new File(location + "/", filename);
				//print("filepath: " + file.getCanonicalPath())
				if(file.isFile()) {return file};
            }
            return null;
        },
		
		Write : function save(data, destination) {
	      var directory, stream;
	      if (!File || !BufferedWriter || !FileWriter) {
	        throw new Error("This operation requires Mozilla Rhino.");
	      }
	      if (!(destination && destination instanceof File)) {
	        destination = new File(destination);
	      }
	      // Resolve relative paths against the Quilt working directory.
	      destination = (destination.isAbsolute() ? destination : new File(
	        buildconfig.ApplicationFolderName, destination.getPath())).getCanonicalFile();
	      // Recursively create all destination directories if they don't exist.
	      directory = destination.getParent();
	      if (!directory) {
	        throw new Error("The destination directory '" + directory.getPath() + "' could not be created.");
	      }
	      stream = new BufferedWriter(new FileWriter(destination));
	      stream.write(data);
	      stream.close();
		  
		  try {
		  	if (OS.indexOf("mac") >= 0) {
				runCommand("sh", "-c", "java -jar framework/buildtools/yuicompressor-2.4.2/yuicompressor-2.4.2.jar " + buildconfig.ApplicationFolderName + "/" + outputPath + " -o " + buildconfig.ApplicationFolderName + "/" + compressedPath + " --line-break 200");
			}
			else if(OS.indexOf("win") >=0){
				runCommand("cmd", "/C", "java -jar framework/buildtools/yuicompressor-2.4.2/yuicompressor-2.4.2.jar " + buildconfig.ApplicationFolderName + "/" + outputPath + " -o " + buildconfig.ApplicationFolderName + "/" + compressedPath + " --line-break 200");
			}
		  } catch(e) {
		  	print("WARNING: Build is ok but unable to minify javascript output.")
		  }
	      return true;
	    }
	};
	return Koncat;
})(this);



var file= Koncat.File("application.js");
var newcode = Koncat.Build(file);
Koncat.Write(newcode, "cdn/src/" + buildconfig.ApplicationFolderName + ".js");
//print(newcode)
print("BUILD SUCCESSFULL\n\n");
//runCommand("sh", "-c", "rake js:runtime", { env: {env1: 100, env2: 200}})
//runCommand("cmd", "/C", "date /T")
